import { defineStore } from 'pinia'
import { reqC1, reqC2, reqC3 } from '@/api/product/attr'
import { ElMessage } from 'element-plus'
import { CategoryResponseData } from '@/api/product/attr/type'
import { CategoryState } from './types/type'

const useCategoryStore = defineStore('Category', {
  state: (): CategoryState => {
    return {
      c1Arr: [],
      c2Arr: [],
      c3Arr: [],
      c1Id: '',
      c2Id: '',
      c3Id: '',
    }
  },
  actions: {
    async getC1() {
      const result: CategoryResponseData = await reqC1()
      if (result.code === 200) {
        this.c1Arr = result.data
      } else {
        ElMessage({
          type: 'error',
          message: result.message,
        })
      }
    },

    async getC2() {
      const result: CategoryResponseData = await reqC2(this.c1Id)
      if (result.code === 200) {
        this.c2Arr = result.data
      } else {
        ElMessage({
          type: 'error',
          message: result.message,
        })
      }
    },

    async getC3() {
      const result: CategoryResponseData = await reqC3(this.c2Id)
      console.log(result, '333')
      if (result.code === 200) {
        this.c3Arr = result.data
      } else {
        ElMessage({
          type: 'error',
          message: result.message,
        })
      }
    },
  },
  getters: {},
})

export default useCategoryStore
